{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 多重继承"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "多重继承，指的是一个类别可以同时从多于一个父类继承行为与特征的功能，`Python` 是支持多重继承的："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class Leaf(object):\n",
    "    def __init__(self, color='green'):\n",
    "        self.color = color\n",
    "\n",
    "class ColorChangingLeaf(Leaf):\n",
    "    def change(self, new_color='brown'):\n",
    "        self.color = new_color\n",
    "\n",
    "class DeciduousLeaf(Leaf):\n",
    "    def fall(self):\n",
    "        print \"Plunk!\"\n",
    "\n",
    "class MapleLeaf(ColorChangingLeaf, DeciduousLeaf):\n",
    "    pass"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在上面的例子中， `MapleLeaf` 就使用了多重继承，它可以使用两个父类的方法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "yellow\n",
      "Plunk!\n"
     ]
    }
   ],
   "source": [
    "leaf = MapleLeaf()\n",
    "\n",
    "leaf.change(\"yellow\")\n",
    "print leaf.color\n",
    "\n",
    "leaf.fall()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果同时实现了不同的接口，那么，最后使用的方法以继承的顺序为准，放在前面的优先继承："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class Leaf(object):\n",
    "    def __init__(self, color='green'):\n",
    "        self.color = color\n",
    "\n",
    "class ColorChangingLeaf(Leaf):\n",
    "    def change(self, new_color='brown'):\n",
    "        self.color = new_color    \n",
    "    def fall(self):\n",
    "        print \"Spalt!\"\n",
    "\n",
    "class DeciduousLeaf(Leaf):\n",
    "    def fall(self):\n",
    "        print \"Plunk!\"\n",
    "\n",
    "class MapleLeaf(ColorChangingLeaf, DeciduousLeaf):\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Spalt!\n"
     ]
    }
   ],
   "source": [
    "leaf = MapleLeaf()\n",
    "leaf.fall()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class MapleLeaf(DeciduousLeaf, ColorChangingLeaf):\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Plunk!\n"
     ]
    }
   ],
   "source": [
    "leaf = MapleLeaf()\n",
    "leaf.fall()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "事实上，这个顺序可以通过该类的 `__mro__` 属性或者 `mro()` 方法来查看："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(__main__.MapleLeaf,\n",
       " __main__.DeciduousLeaf,\n",
       " __main__.ColorChangingLeaf,\n",
       " __main__.Leaf,\n",
       " object)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "MapleLeaf.__mro__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[__main__.MapleLeaf,\n",
       " __main__.DeciduousLeaf,\n",
       " __main__.ColorChangingLeaf,\n",
       " __main__.Leaf,\n",
       " object]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "MapleLeaf.mro()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "考虑更复杂的例子："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class A(object):\n",
    "    pass\n",
    "\n",
    "class B(A):\n",
    "    pass\n",
    "\n",
    "class C(A):\n",
    "    pass\n",
    "\n",
    "class C1(C):\n",
    "    pass\n",
    "\n",
    "class B1(B):\n",
    "    pass\n",
    "\n",
    "class D(B1, C):\n",
    "    pass"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "调用顺序："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[__main__.D, __main__.B1, __main__.B, __main__.C, __main__.A, object]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "D.mro()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
